package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityBuy;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动购买记录信息DAO
 */
@Repository
public class MultilevelActivityBuyDao extends BaseDao {

	private final static String selectMultilevelActivityBuyListSql = "SELECT " +
			"emab.*, " +
			"ema.ACTIVITY_LEVEL, " +
			"ema.ACTIVITY_BOUNTY, " +
			"ema.ACTIVITY_GIFT, " +
			"ema.ACTIVITY_DURATION, " +
			"ema.ACTIVITY_PUBLISH_TIME, " +
			"ema.ACTIVITY_STATE, " +
			"ec.CAR_TITLE, " +
			"ec.CAR_IMAGE_URL, " +
			"ec.CAR_RETAIL_PRICE, " +
			"ec.CAR_GUIDE_PRICE, " +
			"ec.CAR_MILEAGE, " +
			"ec.CAR_AUTH_STATE, " +
			"ec.CAR_LICENCE_DATE, " +
			"ec.CAR_LICENCE_STATE, " +
			"es.SHOP_NAME, " +
			"es.SHOP_SHORT_NAME, " +
			"es.SHOP_MOBILE, " +
			"es.SHOP_ADDRESS, " +
			"es.SHOP_LOGO, " +
			"cu.USER_NICKNAME, " +
			"cu.USER_MOBILE, " +
			"cu.USER_HEAD " +
			"FROM " +
			"erp_multilevel_activity_buy emab, " +
			"erp_multilevel_activity ema, " +
			"erp_car ec, " +
			"erp_shop es, " +
			"x_client.client_user cu " +
			"WHERE " +
			"ema.ACTIVITY_ID = emab.ACTIVITY_ID " +
			"AND ema.CAR_ID = ec.CAR_ID " +
			"AND ema.SHOP_ID = es.SHOP_ID " +
			"AND emab.USER_ID = cu.USER_ID ";
	/**
	 * 查询多级营销活动购买记录列表
	 * @param multilevelActivityBuy
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityBuy> selectMultilevelActivityBuyList(MultilevelActivityBuy multilevelActivityBuy) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityBuyListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityBuy);
		return this.queryForList(sql.toString(), MultilevelActivityBuy.class, params, multilevelActivityBuy);
	}

	private final static String selectMultilevelActivityBuyNumSql = "SELECT COUNT(emab.BUY_ID) FROM erp_multilevel_activity_buy emab WHERE 1=1 ";
	/**
	 * 查询多级营销活动购买记录数量
	 * @param multilevelActivityBuy
	 * @return
	 * @throws Exception
	 */
	public int selectMultilevelActivityBuyNum(MultilevelActivityBuy multilevelActivityBuy) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityBuyNumSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityBuy);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityBuy
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityBuy multilevelActivityBuy) {
		if(multilevelActivityBuy != null) {
			if(StringUtils.isNotEmpty(multilevelActivityBuy.getBuyId())){
				sql.append("AND emab.BUY_ID = ? ");
				params.add(multilevelActivityBuy.getBuyId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityBuy.getActivityId())){
				sql.append("AND emab.ACTIVITY_ID = ? ");
				params.add(multilevelActivityBuy.getActivityId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityBuy.getShareId())){
				sql.append("AND emab.SHARE_ID = ? ");
				params.add(multilevelActivityBuy.getShareId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityBuy.getUserId())){
				sql.append("AND emab.USER_ID = ? ");
				params.add(multilevelActivityBuy.getUserId());
			}
			if(multilevelActivityBuy.getBuyState() != null){
				sql.append("AND emab.BUY_STATE = ? ");
				params.add(multilevelActivityBuy.getBuyState());
			}
		}
	}
}
